package cn.yto.print.config;

import cn.yto.print.constants.AssistantConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.websocket.Session;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class WeightLinkedContext {

    private static final Logger logger = LoggerFactory.getLogger(WeightLinkedContext.class);

    private static final ConcurrentHashMap<String, Session> WEIGHT_SCOKET_MAP = new ConcurrentHashMap<>();

    private static Integer wmsStatus = AssistantConstants.DISCONNECT;

    private static Integer deviceStatus = AssistantConstants.DISCONNECT;

    public static void addSession(String sessionId, Session session) {
        WEIGHT_SCOKET_MAP.put(sessionId, session);
        updateWmsConnect();
        logger.info("前端称重通道连接建立，会话ID：{}，连接数 : {}", sessionId, getSessionSize());
    }

    public static void removeSession(String sessionId) {
        WEIGHT_SCOKET_MAP.remove(sessionId);
        updateWmsConnect();
    }

    public static Map<String, Session> getAllSession() {
        return WEIGHT_SCOKET_MAP;
    }

    public static int getSessionSize() {
        return WEIGHT_SCOKET_MAP.size();
    }

    public static Integer getWmsConnectStatus() {
        return wmsStatus;
    }

    public static void setWmsConnectStatus(Integer value) {
        wmsStatus = value;
    }

    private static void updateWmsConnect() {
        wmsStatus = getSessionSize() > 0 ? AssistantConstants.CONNECTED : AssistantConstants.DISCONNECT;
    }

    public static Integer getDeviceConnectStatus() {
        return deviceStatus;
    }

    public static void setDeviceConnectStatus(Integer value) {
        deviceStatus = value;
    }

    public static void resetDeviceConnect() {
        deviceStatus = AssistantConstants.DISCONNECT;
    }

}
